package com.honkentuber.chat;

import org.json.simple.JSONObject;

import com.google.appengine.api.datastore.KeyFactory;
import com.honkentuber.AppContext;
import com.honkentuber.Servlet;

public class DeleteUserServlet extends Servlet {
  private static final long serialVersionUID = 7580743441703466765L;

  private static String deleteUser(AppContext ctx) {
    String key = ctx.req.getParameter("key");
    long count = ctx.db.deleteUser(KeyFactory.stringToKey(key));
    String response = count >= 0
        ? ("Deleted " + key + " and " + count + " messages")
        : ("User not found");
    return response;
  }

  @Override
  protected String newJspResponse(AppContext ctx) {
    String response = deleteUser(ctx);
    ctx.req.setAttribute("response", response);
    return "/response.jsp";
  }

  @Override
  @SuppressWarnings("unchecked")
  protected JSONObject newJsonResponse(AppContext ctx) {
    String response = deleteUser(ctx);
    JSONObject jsonResponse = new JSONObject();
    jsonResponse.put("response", response);
    return jsonResponse;
  }
}